<template>
  <div class="layout-pd">
    <zh-card title="查询条件" v-if="pageData.showType">
      <zh-form :model="pageData.queryParams" ref="queryFormRef" label-width="160px" :span="8">
        <template v-if="pageData.advanced">
        </template>
        <zh-form-query v-model="pageData.advanced" :tableRef="queryTable" :formRef="queryFormRef"> </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="查询结果" ref="queryTable" size="mini" funcCode="${funcCode}0001" :columns="pageData.tableColumns" highlight-current-row>
      <template #header_button>
        <zh-button button-type="add" @click="showAdd()" v-func:${funcCode}0004>新增</zh-button>
      </template>
      <template #flag_default="{ row }">
        <zh-badge :status="statusMap[row.status].status" :text="statusMap[row.status].text" />
      </template>
      <template #operate="{ row }">
        <zh-button v-func:${funcCode}0002 link @click="showdetail(row, true)" style="color: green">查看</zh-button>
        <zh-button v-func:${funcCode}0005 link @click="showdetail(row, false)">修改</zh-button>
        <zh-button v-func:${funcCode}0006 link @click="deleteData(row)" style="color: red">删除</zh-button>
      </template>
    </zh-table>

    <zh-dialog style="width: 80%" :title="pageData.title" v-model="pageData.detailVisible">
      <${funcCode}Form
          ref="menuRef"
          :disabled="pageData.editFlag"
          @canner="canner"
          @refreshTable="refreshTable"
          :${id}="pageData.selectRow.${id}"
      ></${funcCode}Form>
      <template #footer>
				<span class="dialog-footer">
					<zh-button @click="pageData.detailVisible = false">关闭</zh-button>
					<zh-button
              type="primary"
              :loading="pageData.saveLoading"
              :disabled="pageData.saveLoading"
              v-model:btnLoding="pageData.saveLoading"
              @click="save"
          >
						保存
					</zh-button>
				</span>
      </template>
    </zh-dialog>
  </div>
</template>
<script lang="ts" setup name="${funcCode}Index">
import { ElMessageBox, ElMessage } from 'element-plus';
import ${funcCode}Form from './component/${funcCode}Form.vue';

// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
});

// ------------------定义子组件--------------------------
const queryTable = ref();
const queryFormRef = ref();
const menuRef = ref();

const statusMap = {
  0: {
    status: 'error',
    text: '无效',
  },
  1: {
    status: 'processing',
    text: '有效',
  },
};

// ------------------变量区------------------------------
const pageData = reactive({
  advanced: false,

  tableColumns: [
    {
      type: 'seq',
      minWidth: 60,
      fixed: 'left',
      align: 'center',
      title: '序号',
    },
    <#list table.fields as field>
    <#if field.keyFlag>
    {
      field: '${id}',
      title: '主键',
      visible: false,
      align: 'center',
      minWidth: 80,
    },
    <#elseif "createName" == field.propertyName || "createTime" == field.propertyName || "updateName" == field.propertyName || "updateTime" == field.propertyName || "del" == field.propertyName>
    <#else>
    {
      field: '${field.propertyName}',
      title: '${field.comment}',
      visible: true,
      align: 'center',
      minWidth: 150,
    },
    </#if>
    </#list>
    {
      field: 'operate',
      title: '操作',
      fixed: 'right',
      align: 'center',
      minWidth: 210,
      slots: {
        // 使用插槽模板渲染
        default: 'operate',
      },
    },
  ],
  queryParams: {} as any,
  dictData: {} as any,
  title: '',
  detailVisible: false,
  editFlag: false,
  selectRow: {} as any,
  saveLoading: false,
  showType: true,
  campusParam: {} as any,
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  refreshTable();
};
// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = '';
  $api.common.getBatchDict.post(params).then((res) => {
    pageData.dictData = res.result;
  });
};

//使用子组件方法，关闭弹窗
const canner = (e: any) => {
  pageData.detailVisible = e;
};

const showAdd = () => {
  pageData.title = '新增';
  pageData.detailVisible = true;
  pageData.editFlag = false;
  pageData.selectRow = {};
};

const showdetail = (row, flag) => {
  if (!flag) {
    pageData.title = '信息修改';
  } else {
    pageData.title = '查看详情';
  }
  pageData.selectRow = row;
  pageData.editFlag = flag;
  pageData.detailVisible = true;
};
//保存
const save = () => {
  menuRef.value.saveBase();
};

//刷新
const refreshTable = () => {
  queryTable.value.refresh(pageData.queryParams);
};
//删除
const deleteData = (row) => {
  ElMessageBox.confirm('是否确认删除？').then(() => {
    let params = {} as any;
    params.${id} = row.${id};
    $api.common.send.post('${funcCode}0006', params).then(() => {
      ElMessage.success('删除成功');
      refreshTable();
    });
  });
};

</script>
<style lang="scss" scoped></style>
